package com.example.rydwlib.fragment

import android.view.View
import android.widget.*
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.listener.OnOptionsSelectListener
import com.bigkoo.pickerview.view.OptionsPickerView
import com.example.baselib.dialog.BaseDialogFragment
import com.example.rydwlib.R
import com.example.rydwlib.adapter.selectAdapter
import com.example.rydwlib.bean.RyPersonData
import kotlinx.android.synthetic.main.dialog_select.*
import kotlinx.android.synthetic.main.dialog_select.view.*
import android.view.LayoutInflater
import android.view.ViewGroup
import android.graphics.drawable.BitmapDrawable
import androidx.recyclerview.widget.DividerItemDecoration






/**
 * Author: sxygsj
 * Date: 2020/6/9 17:11
 * Description: 选择dialog
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
class SelectDialog(var list: List<RyPersonData>) :BaseDialogFragment(){
    override fun getContentLayout(): Int = R.layout.dialog_select
    private var callback: onClickCallback? = null
    var popupWindow: PopupWindow? = null
    val adapter =selectAdapter(R.layout.select_item)
    var deptlist= mutableListOf<String>()
    var postlist= mutableListOf<String>()
    var regionlist= mutableListOf<String>()
    var stationlist= mutableListOf<String>()
    private lateinit var pickerView: OptionsPickerView<Any>
    override fun initView(view: View) {
        view.select_sure.setOnClickListener {
            val map:HashMap<String,String> = HashMap()
            map["department"] = ed_deportment.text.toString()
            map["post"]=ed_post.text.toString()
            map["region"]=ed_region.text.toString()
            map["station"]=ed_station.text.toString()
            map["namecard"]=ed_namecard.text.toString()
            callback?.click(map)
            dismiss()
        }
        view.select_cancel.setOnClickListener {
//            callback?.clear()
            dismiss()
        }
//        initNickPicker()
        val layoutInflater = LayoutInflater.from(context)
        // 加载一个布局文件 这个布局文件就是popwindow显示时的样子
        var inflate = layoutInflater.inflate(R.layout.pop, null)
        // 创建一个popwindow，将布局文件和宽高以及是否获取焦点设置好
        popupWindow = PopupWindow(inflate , 650 , ViewGroup.LayoutParams.WRAP_CONTENT , true)
        popupWindow!!.setBackgroundDrawable(BitmapDrawable())
        popupWindow!!.isOutsideTouchable=true
        val recyclerView = inflate.findViewById<RecyclerView>(R.id.select_rcv)
        recyclerView.adapter=adapter
        recyclerView.layoutManager= LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        recyclerView.addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL))
        view.ed_deportment.setOnClickListener {
            adapter.setNewData(deptlist)
            adapter.setOnItemClickListener { adapter, view, position ->
               ed_deportment.text=adapter.getItem(position) as String
                popupWindow!!.dismiss()
            }
            popupWindow!!.showAsDropDown(it)
        }
        view.ed_post.setOnClickListener {
            adapter.setNewData(postlist)
            adapter.setOnItemClickListener { adapter, view, position ->
                ed_post.text=adapter.getItem(position) as String
                popupWindow!!.dismiss()
            }
            popupWindow!!.showAsDropDown(it)
        }
        view.ed_region.setOnClickListener {
            adapter.setNewData(regionlist)
            adapter.setOnItemClickListener { adapter, view, position ->
                ed_region.text=adapter.getItem(position) as String
                popupWindow!!.dismiss()
            }
            popupWindow!!.showAsDropDown(it)
        }
        view.ed_station.setOnClickListener {
            adapter.setNewData(stationlist)
            adapter.setOnItemClickListener { adapter, view, position ->
            ed_station.text=adapter.getItem(position) as String
                popupWindow!!.dismiss()
            }
            popupWindow!!.showAsDropDown(it)
        }
    }

    override fun initData() {
        var deptSet= linkedSetOf<String>()
        var postSet= linkedSetOf<String>()
        var regionSet= linkedSetOf<String>()
        var stationSet= linkedSetOf<String>()
        for (p in list){
            deptSet.add(p.department)
            postSet.add(p.personPost)
            regionSet.add(p.regionname)
            stationSet.add(p.stationname)
        }
        deptlist.addAll(deptSet)
        postlist.addAll(postSet)
        regionlist.addAll(regionSet)
        stationlist.addAll(stationSet)
    }
    fun setCallbackListener(callback: onClickCallback) : SelectDialog {
        this.callback=callback
        return this
    }
    private fun initNickPicker() {
        pickerView = OptionsPickerBuilder(activity,
            OnOptionsSelectListener { options1, options2, options3, v ->

            })
//            .isDialog(true)
            .setContentTextSize(18)
            .setTextColorCenter(resources.getColor(R.color.colorDBlue))
            .setItemVisibleCount(9)
            .setLineSpacingMultiplier(2.6.toFloat())
            .setBackgroundId(R.color.colorBlack)
            .build()

    }
//    override fun setDialogStyle() {
//        //无标题
//        dialog?.requestWindowFeature(STYLE_NO_TITLE)
//        //点击外部不消失
//        dialog?.setCanceledOnTouchOutside(false)
//        //设置 样式
//        dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
//        dialog?.window?.decorView?.setPadding(0, 0, 0, 0)
//        dialog?.window?.attributes?.width= ListPopupWindow.MATCH_PARENT
//        dialog?.window?.attributes?.height= ListPopupWindow.MATCH_PARENT
//        dialog?.window?.attributes?.gravity= Gravity.CENTER
//
//    }

    interface onClickCallback {
        fun click(map:HashMap<String,String>)
//        fun clear()
    }
}